export function computeOffsetPixels(
  offset: number|string,
  contextHeight: number
):number{
  const pixelOffset = parseOffsetAsPixel(offset);
  if(typeof pixelOffset === 'number'){
    return pixelOffset;
  }
  if(typeof offset === 'string'){
    const percentOffset = parseOffsetAsPercentage(offset);
    if(typeof percentOffset === 'number'){
      return percentOffset * contextHeight;
    }
  }
}

function parseOffsetAsPercentage(str: string): number|undefined{
  if(str.slice(-1) === '%'){
    return parseFloat(str.slice(0,-1)) / 100;
  }
  return undefined;
}


function parseOffsetAsPixel(str: string|number):number|undefined{
  let val:number;
  if(typeof str === 'number'){
    val = str;
  }else{
    val = parseFloat(str);
  }

  if(!Number.isNaN(val) && Number.isFinite(val)){
    return val;
  }
  return undefined;
}
